package com.jdjw.utils;


import com.alibaba.fastjson.serializer.ValueFilter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CommonUtil {

    /**
     * 计算切分次数
     *
     * @param size   需要分割的集合长度
     * @param maxNum 集合最大的数量
     * @param <T>
     * @return 切分次数
     */

    private static Integer countStep(Integer size, Integer maxNum) {

        return (size + maxNum - 1) / maxNum;

    }


    /**
     * 将一个集合进行分割
     *
     * @param collections 需要分割的集合 (List LinkedHashSet TreeSet LinkedList)
     * @param maxNum      集合最大的数量
     * @param <T>
     * @return 分割后的集合
     */

    public static <T> Collection<Collection<T>> subCollection(Collection<T> collections, int maxNum) {


        int limit = countStep(collections.size(), maxNum);

//方法一：使用流遍历操作

// Collection<Collection<T>> mglist = new ArrayList<>();

// Stream.iterate(0, n -> n + 1).limit(limit).forEach(i -> {

// mglist.add(collections.stream().skip(i * maxNum).limit(maxNum).collect(Collectors.toList()));

// });

// return mglist;


//方法二：获取分割后的集合

        Collection<Collection<T>> splitCollection = Stream.iterate(

                0, n -> n + 1).limit(limit).parallel().map(

                a -> collections.stream().skip(a * maxNum).limit(maxNum).parallel()

                        .collect(Collectors.toList())).collect(Collectors.toList());


        return splitCollection;

    }

    public static String addZeroForNum(String str, int strLength) {
        int strLen = str.length();
        StringBuffer sb = null;
        while (strLen < strLength) {
            sb = new StringBuffer();
            //sb.append("0").append(str);// 左补0
            sb.append(str).append("0");//右补0
            str = sb.toString();
            strLen = str.length();
        }
        return str;
    }


    private static ObjectMapper objectMapper = new ObjectMapper();
    static {
        //忽略属性为null
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        //忽略多余属性
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public static String objectToString(Object object)  {

        try {
            return objectMapper.writeValueAsString(object);

        } catch (JsonProcessingException e) {
            // TODO: handle exception
        }
        return null;
    }

    public static <T> T stringToObject(String json,Class<T> object) throws IOException {
        return objectMapper.readValue(json,object);
    }

    public static <T> List<T>   stringToList(String json,Class<T> object){
        try {
            CollectionType listType = objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, object);
            List<T> list = objectMapper.readValue(json, listType);
            return list;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static ValueFilter filter = new ValueFilter() {
        @Override
        public Object process(Object obj, String s, Object v) {
            if(v==null)
                return "";
            return v;
        }
    };

}
